package com.test.platform.module.system.controller;

import com.test.common.entity.system.User;
import com.test.common.enums.system.UserStatusEnum;
import com.test.common.form.system.LoginForm;
import com.test.common.form.system.UserFrom;
import com.test.platform.module.system.service.TokenService;
import com.test.platform.module.system.service.UserService;
import com.test.common.response.BizException;
import com.test.common.response.ResponseResult;
import com.test.common.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Api(tags = "系统用户管理")
@RequestMapping("/system/user")
@RestController
@ResponseResult
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private TokenService tokenService;

    @ApiOperation("用户登录")
    @PostMapping("login")
    public Object login(@RequestBody LoginForm loginForm) {

        log.info("loginForm|{}", loginForm);

        // 用户信息
        User user = userService.findByName(loginForm.getUsername());
        log.info("user|{}", user);
        // 账号不存在、密码错误
        if (user == null || !user.getPasswd().equals(loginForm.getPassword())) {
            return new BizException(ResultCode.USER_LOGIN_ERROR.code(), ResultCode.USER_LOGIN_ERROR.message());
        }
        // 账号锁定
        if (user.getStatus().equals(UserStatusEnum.LOCKED)) {
            return new BizException(ResultCode.USER_ACCOUNT_LOCKED.code(), ResultCode.USER_ACCOUNT_LOCKED.message());
        }

        // 生成token，并保存到数据库
        return tokenService.create(user.getId());
    }

    @ApiOperation("用户退出")
    @GetMapping("logout")
    public Object logout() {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (tokenService.delete(user.getId())) {
            SecurityUtils.getSubject().logout();
            return "退出成功";
        } else {
            throw new BizException("用户不存在");
        }
    }

    @ApiOperation("用户信息")
    @GetMapping("info")
    public Object userInfo() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> data = new HashMap<>();
        data.put("name", user.getName());
        data.put("avatar", user.getAvatar());
        data.put("roles", new String[]{"admin"});
        return data;
    }

    @ApiOperation("创建用户")
    @PostMapping("add")
    public Object userAdd(@RequestBody UserFrom userAddFrom) {
        log.info("user add|{}", userAddFrom);
        return userService.create(new User(userAddFrom.getUserName(), userAddFrom.getPassWord(), userAddFrom.getAvatar()));
    }

    @ApiOperation("删除用户")
    @GetMapping("remove")
    public Object userRemove(Integer userId) {
        log.info("user remove|{}", userId);
        return userService.delete(userId);
    }

    @ApiOperation("更新用户")
    @PostMapping("update")
    public Object update(User user) {
        log.info("update|{}", user);
        return userService.update(user);
    }

    @ApiOperation("用户列表")
    @GetMapping("list")
    public Object list(int pageNo, int pageSize) {
        log.info("list|{}|{}", pageNo, pageSize);
        return userService.findAll(pageNo, pageSize);
    }

    @GetMapping("all")
    public Object all() {
        log.info("查询所有用户");
        List<User> all = userService.findAll();
        List<HashMap<String, String>> result = new ArrayList<>();
        all.forEach(user -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", user.getName());
            result.add(map);
        });
        return result;
    }
}
